/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <QComboBox>
#include <QDockWidget>
#include <QMainWindow>
#include <QIcon>
#include <QPixmap>
#include <QAction>

#include "controlbar.h"
#include "../constants.h"

ControlBar::ControlBar(QMainWindow *ptr) {
  dock = new QDockWidget(QObject::tr("Bediening"), ptr);
  widget = new QWidget;

  // Remove unneeded stupid features:
  dock->setFeatures(QDockWidget::DockWidgetClosable);
  dock->setFeatures(QDockWidget::DockWidgetMovable);

  // Initialize the layout
  layout = new QHBoxLayout;

  // The combobox for the channels.
  teleTekstBox = new QComboBox();

  // Prev button
  prevButton = new QPushButton(ptr);
  prevButton->setText("P");
  prevButton->setShortcut(QObject::tr("b"));

  // Next button
  nextButton = new QPushButton(ptr);
  nextButton->setText("N");
  nextButton->setShortcut(QObject::tr("f"));

  // Up button
  upButton = new QPushButton(ptr);
  upButton->setText("U");
  upButton->setShortcut(QObject::tr("n"));

  // Down button
  downButton = new QPushButton(ptr);
  downButton->setText("D");
  downButton->setShortcut(QObject::tr("p"));

  // Page setter
  pageid = new QSpinBox(ptr);
  pageid->setRange(100, 999);
  pageid->setValue(TTBQT_DEF_PAGE);

  // Dirty hack to allow a user to press 'space' followed by a page id
  pfocus = new QPushButton(ptr);
  pfocus->setShortcut(Qt::Key_Space);

  // Sub page setter
  subpageid = new QSpinBox(ptr);
  subpageid->setRange(1, TTBQT_MAX_SUB_PAGE);
  subpageid->setValue(1);

  // Quit button
  quitButton = new QPushButton(ptr);
  quitButton->setText("Q");
  quitButton->setShortcut(QObject::tr("q"));

  // Help button
  helpButton = new QPushButton(ptr);
  helpButton->setText("H");
  helpButton->setShortcut(QObject::tr("h"));

  // Add everything to the widget
  dock->setWidget(widget);

  layout->addWidget(prevButton);
  layout->addWidget(pageid);
  layout->addWidget(nextButton);
  layout->addWidget(upButton);
  layout->addWidget(subpageid);
  layout->addWidget(downButton);
  layout->addWidget(teleTekstBox);
  layout->addWidget(helpButton);
  layout->addWidget(quitButton);

  widget->setLayout(layout);
}

ControlBar::~ControlBar() {
  delete dock;
  delete downButton;
  delete helpButton;
  delete layout;
  delete nextButton;
  delete pageid;
  delete prevButton;
  delete quitButton;
  delete subpageid;
  delete teleTekstBox;
  delete upButton;
  delete widget;
}

QDockWidget *ControlBar::getWidget() {
  return dock;
}

void ControlBar::addItem(QString& item) {
  teleTekstBox->addItem(item);
}

// Connect the signal. Pointer to the class that has the SLOT function
void ControlBar::connect(Controller *ptr, QApplication *mwn) {
  QObject::connect(downButton, SIGNAL(clicked()), ptr, SLOT(downPressed()));
  QObject::connect(helpButton, SIGNAL(clicked()), ptr, SLOT(helpPressed()));
  QObject::connect(nextButton, SIGNAL(clicked()), ptr, SLOT(nextPressed()));
  QObject::connect(pageid, SIGNAL(valueChanged(int)), ptr, SLOT(teleTekstPageChanged(int)));
  QObject::connect(prevButton, SIGNAL(clicked()), ptr, SLOT(prevPressed()));
  QObject::connect(quitButton, SIGNAL(clicked()), mwn, SLOT(quit()));
  QObject::connect(subpageid, SIGNAL(valueChanged(int)), ptr, SLOT(teleTekstSubPageChanged(int)));
  QObject::connect(teleTekstBox, SIGNAL(currentIndexChanged(const QString &)), ptr, SLOT(teleTekstChannelChanged(const QString &)));
  QObject::connect(upButton, SIGNAL(clicked()), ptr, SLOT(upPressed()));
  // Note: Part of the above hack
  QObject::connect(pfocus, SIGNAL(clicked()), ptr, SLOT(setPageFocus()));

  // Make sure the controller has a pointer of the controlbar, so it can update the page counter
  ptr->setPageSpinBox(pageid);
  ptr->setSubPageSpinBox(subpageid);
}
